<?php
namespace app\shop\controller;

use think\Controller;

class Index extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->TypeModel = model("Product.Type");
        $this->ProductModel = model("Product.Product");
        $this->CartModel = model("Product.Cart");
    }
    public function index()
    {
        // 查询分类
        $typelist = $this->TypeModel->order('weigh asc')->limit(7)->select();

        // 查询推荐
        $banner = $this->ProductModel->where(['flag' => '3'])->limit(8)->select();

        // 查询新品
        $news = $this->ProductModel->where(['flag' => '1'])->find();

        // 查询新品
        $hots = $this->ProductModel->where(['flag' => '2'])->limit(8)->select();

        //获取站点名称
        $site = config('site.name');

        // 组装数据
        $data=[
            'typelist' => $typelist,
            'banner' => $banner,
            'news' => $news,
            'hots' => $hots,
            'site' => $site
        ];
        $this->success('首页数据', null, $data);
        exit;
    }

    // 商品列表
    public function list()
    {
        if($this->request->isPost())
        {
            // 接收参数
            $typeid=$this->request->param('typeid',0,'trim');
            $page=$this->request->param('page',1,'trim');
            $flag=$this->request->param('flag',0,'trim');
            $sort=$this->request->param('sort','createtime','trim');
            $by=$this->request->param('by','desc','trim');
            $keywords=$this->request->param('keywords','','trim');
            $limit=10;
            $start=($page-1)*$limit;
            $TypeName='全部商品';

            // 条件判断
            $where=[];

            if($typeid)
            {
                $where['typeid']=$typeid;
                $TypeName=$this->TypeModel->where(['id'=>$typeid])->value('name');
            }

            if($flag)
            {
                $where['flag']=$flag;
            }

            // 搜索
            if(!empty($keywords))
            {
                $where['product.name']=['LIKE',"%$keywords%"];
            }

            $list=$this->ProductModel->with(['type', 'unit'])->where($where)->order("$sort $by")->limit($start,$limit)->select();

            // var_dump($this->ProductModel->getLastSql());
            // exit;

            $data=[
                'TypeName'=>$TypeName,
                'list'=>$list
            ];

            // 返回数据
            if(count($list)<=0)
            {
                $this->error('暂无更多数据',null,$data);
                exit;
            }else
            {
                $this->success('返回数据成功',null,$data);
                exit;
            }
            
        }
    }

    // 商品分类
    public function type()
    {
        if($this->request->isPost())
        {
            $type=$this->TypeModel->select();
            $this->success('返回商品分类数据',null,$type);
            exit;
        }
    }

    // 商品详情
    public function info()
    {
        if($this->request->isPost())
        {
            $id=$this->request->param('id',0,'trim');
            $busid=$this->request->param('busid',0,'trim');
            
            //查询商品是否存在
            $product=$this->ProductModel->with(['type', 'unit'])->find($id);

            // 查询购物车数量
            $count=$this->CartModel->where(['busid'=>$busid])->sum('nums');

            // 查询客服电话
            $contact=config('site.contact');

            // 组装数据
            $data=[
                'product'=>$product,
                'count'=>$count,
                'contact'=>$contact,
            ];

            if($product===false)
            {
                $this->error('暂无该商品');
                exit;
            }else
            {
                $this->success('商品数据',null,$data);
                exit;
            }
        }
    }
}